package Sample;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import glue.*;
/******
I wouldn't normally put so much stuff in one file but I don't think it's that difficult 
to read. You should find the SuperFileFilter and RecentProjects objects. To use each is 
only a few lines of code. To use, just create a few files with the NewFile menu option 
then switch between those files by selecting them form the recent projects section of the 
file menu.
*******/
public class Sample extends JFrame 
{
	JTextArea txt=new JTextArea();
	private MenuBar menuBar = null;
	private File file = null;
	
	public class MenuBar extends JMenuBar 
	{

		public class RecentProjectMenuItem extends JMenuItem implements ActionListener  
		{
	
			File file = null;
			Sample parent = null;
			
			public RecentProjectMenuItem(Sample parent, String text, File file)
			{
				super(text);
				this.parent = parent;
				this.file = file;
				addActionListener(this);
			}
		
			public void actionPerformed(ActionEvent a0)
			{
				Open(file);
			}
			
			private void Open(File f){
				StringBuilder buffer = null;
				try{
				    buffer = new StringBuilder();
					BufferedReader r = new BufferedReader(new FileReader(f.getPath()));
			 		int val = 0;
			 		while( (val = r.read()) != -1){
				 		buffer.append((char)val);
			 		}
			 		r.close();
			 		parent.setFile(f);
			 		parent.setText(buffer.toString());
			 		parent.setTitle(new String("Sample (" + f.getPath() + ")"));
				}catch(IOException io){
					System.err.print(io.getMessage());
				}
			}
		}		
		
				
		public class EventSaveFile extends JMenuItem implements ActionListener 
		{
			Sample parent = null;
			public EventSaveFile(String text, Sample parent)
			{
				super(text);
				this.parent = parent;
				addActionListener(this);
			}
		
			public void actionPerformed(ActionEvent a0)
			{
				try{
					FileWriter b = new FileWriter(parent.getFile());
					BufferedWriter w = new BufferedWriter(b);
					w.write(parent.getText(), 0, parent.getText().length());
					w.close();
					b.close();
					
					w = null;
					b = null;
					
				}catch(IOException io){
					System.err.print(io.getMessage());
				}
			}
		}
		
		
		
		
		public class EventNewFile extends JMenuItem implements ActionListener 
		{
			Sample parent = null;
			public EventNewFile(String text, Sample parent)
			{
				super(text);
				this.parent = parent;
				addActionListener(this);
			}
		
			public void actionPerformed(ActionEvent a0)
			{
				JFileChooser chooser = new JFileChooser();
				chooser.setDialogTitle("New File");
//				chooser.setCurrentDirectory();
		
				chooser.setMultiSelectionEnabled(false);
				chooser.addChoosableFileFilter(new SuperFileFilter(new String("HTML"), new String("htm,html")));
				chooser.addChoosableFileFilter(new SuperFileFilter(new String("Java server page"), new String("jsp")));
				chooser.setAcceptAllFileFilterUsed(false);
		
				int returnVal = chooser.showSaveDialog(parent);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					String ext = null;
					SuperFileFilter filter = (SuperFileFilter)chooser.getFileFilter();
					if(filter.getPossibleExtensions().contains("htm")){
						ext = new String(".htm");
					}else if(filter.getPossibleExtensions().contains("jsp")){
						ext = new String(".jsp");
					}else{
						ext = new String(".htm");
					}
					
					File f = new File(chooser.getSelectedFile().getPath() + ext);
					try{
						f.createNewFile();
					}catch (IOException e){
						System.err.println("failed to create file:" + e.getMessage());
					}
					
					parent.setFile(f);
					parent.setTitle(new String("Sample (" + f.getPath() + ")"));
					RecentProjects rp = RecentProjects.open();
					rp.addProject(file.getName(),f);
					RecentProjects.save(rp);
					parent.refreshMenu();
				}
			}
		}						
		
		public class EventExit extends JMenuItem implements ActionListener 
		{
			Sample parent = null;
			public EventExit(String text, Sample parent)
			{
				super(text);
				this.parent = parent;
				addActionListener(this);
			}
		
			public void actionPerformed(ActionEvent a0)
			{
				System.exit(0);
			}
		}
		
		public MenuBar(Sample parent)
		{
			createMenu(parent);
		}
	
		public void createMenu(Sample parent){
			
			JMenu fileMenu = new JMenu("File");
			add(fileMenu);
			fileMenu.add(new EventNewFile(new String("New File..."), parent));
			fileMenu.add(new EventSaveFile(new String("Save"), parent));
			fileMenu.addSeparator();
			
			RecentProjects rp = RecentProjects.open();
			Object list[] = rp.getProjectNames();
			for(Object o : list){
				fileMenu.add( new RecentProjectMenuItem(parent, o.toString(), rp.getProjectPath(o.toString())));
			}			
		
			fileMenu.addSeparator();
			fileMenu.add(new EventExit(new String("Exit"), parent));
		}
	}
	
	public Sample()
	{
		initializeMethod();
		menuBar = new MenuBar(this);
		setJMenuBar(menuBar);
	}
	
	public void setFile(File f){
		file = f;
	}
	
	public File getFile(){
		return file;
	}
	
	public void setText(String buffer){
		txt.setText(buffer);
	}
	
	public String getText(){
		return txt.getText();
	}
	
	public void refreshMenu(){
		menuBar.removeAll();
		menuBar.createMenu(this);
		setJMenuBar(menuBar);
	}

	private void initializeMethod()
	{
		Container container=getContentPane();
		container.setLayout(new BorderLayout());

		container.add(txt,"Center");

		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				dispose();
				System.exit(0);
			}
		});
	}

	public static void main(String[] sCommand)
	{
		Sample app=new Sample();
		app.setBounds(133,100,532,400);
		app.setTitle("Sample");
		app.setVisible(true);
	}

}

